package com.wehealth.three.lib_connect.device.usbdevice;

import android.hardware.usb.UsbDeviceConnection;
import android.os.Handler;
import android.os.Message;

import com.wehealth.three.lib_connect.device.usbdevice.data.ParseOriginEcgData;
import com.wehealth.three.lib_connect.device.usbdevice.port.UsbSerialPort;
import com.wehealth.three.lib_connect.utils.PreferenceUtils;

import java.io.IOException;

/****
 * 二代机的usb传输的处理
 */
public class UsbDeviceInfoThread extends Thread {

    //usb设备的读取超时时间
    private static final int READ_WAIT_MILLIS = 50;
    //每次从串口读取数据的数据量
    private static final int BUFFERSIZE = 31;

    public static final int USB_DEVICE_GET_DEVICE_INFO_START = 1000;
    public static final int USB_DEVICE_GET_DEVICE_INFO_START_SUCCESS = 1001;
    public static final int USB_DEVICE_GET_DEVICE_INFO_START_FAILURE = 1002;


    UsbDeviceConnection usbDConn;
    UsbSerialPort usPort;
    ParseOriginEcgData ecgParser24;
    Handler handler;


    public UsbDeviceInfoThread(UsbDeviceConnection usbConnection, UsbSerialPort usbSerialPort, Handler handle, ParseOriginEcgData.EcgDataGetListener ecgDataListener) {
        usbDConn = usbConnection;
        usPort = usbSerialPort;
        ecgParser24 = new ParseOriginEcgData(ecgDataListener);
        ecgParser24.ecgDataParserInit();
        handler = handle;

    }

    @Override
    public void run() {
        super.run();
        try {
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            usPort.open(usbDConn);
            //配置串口参数
            usPort.setParameters(921600, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
            usPort.setDTR(true);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //开始接受数据绘制
            handler.sendEmptyMessage(USB_DEVICE_GET_DEVICE_INFO_START);

            //通过串口发送开始命令
            usPort.write(ParseOriginEcgData.packEcgDeviceInfoCmd(), READ_WAIT_MILLIS);

            //读取数据的缓存的大小
            byte[] data = new byte[BUFFERSIZE];

            //读取数据
            usPort.read(data, READ_WAIT_MILLIS);
            if (!ecgParser24.EcgParserCMDInfo(data)) {
                //TODO 数据解析出错
                Message msg = handler.obtainMessage(USB_DEVICE_GET_DEVICE_INFO_START_FAILURE);
                msg.obj = "数据解析出现异常,未能正确处理数据";
                handler.sendMessage(msg);
            }
            String usbDeviceSerialNo = PreferenceUtils.getInstance().getTempSerialNo();
            Message msg = handler.obtainMessage(USB_DEVICE_GET_DEVICE_INFO_START_SUCCESS);
            msg.obj = usbDeviceSerialNo;
            handler.sendMessage(msg);

        } catch (IOException e) {
            if (!e.getMessage().equals("change_error")) {
                e.printStackTrace();
                Message msg = handler.obtainMessage(USB_DEVICE_GET_DEVICE_INFO_START_FAILURE);
                msg.obj = "数据传输出现异常,未能正确获取数据";
                handler.sendMessage(msg);
            }

        }
    }


    /***
     * 停止串口的数据传输
     */
    public void stopSerial() {
        if (usPort != null) {
            try {
                ecgParser24.stopInit();
                //先阻塞线程
                Thread.sleep(100);
                //调用interrupt()触发异常结束线程
                interrupt();
            } catch (InterruptedException e) {
                usbDConn.close();
                usbDConn = null;
                try {
                    usPort.close();
                    usPort = null;
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }

            }
        }
    }

}
